﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using View.BookForms;
using Model;

namespace View.MemberForms
{
    public partial class MemberEditForm : Form
    {
        MemberEventArgs _properties;

        ILibraryModel _model;

        public MemberEditForm()
        {
            InitializeComponent();
        }

        private void buttonBorrowReturn_Click(object sender, EventArgs e)
        {
            Controls2Fields();
            if (0 != dataGridViewBorrowings.SelectedRows.Count)
            {
                OnReturnBook();
            }
            UpdateBorrowings();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            Controls2Fields();
            OnSubmit();
            SafeClose();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            SafeClose();
        }

        private void SafeClose()
        {
            _properties = null;
            Hide();
        }

        public void UpdateBorrowings()
        {
            var _editedMember = _model.GetAllMembers().First(m => (m.GetId() == _properties.Id));
            dataGridViewBorrowings.DataSource = _editedMember.Borrowings.Select(m => new
            {
                Id = m.GetId().ToString(),
                BookId = m.Book.GetId().ToString(),
                Author = (m.Book.Author ?? ""),
                Title = (m.Book.Title ?? ""),
                BorrowingDate = (m.BorrowedDate.Value.ToShortDateString()),
                ReturnDate = ((m.ReturnedDate != null) ? m.ReturnedDate.Value.ToShortDateString() : ""),
                Deadline = ((DateTime) (m.BorrowedDate + _editedMember.GetBorrowDurationLimit())).ToShortDateString()
            }).ToList();
        }


        public void ShowDialog(ILibraryModel model, MemberEventArgs fields)
        {
            _model = model;
            _properties = fields;
            Fields2Controls();
            if (null == _properties.Id)
            {
                groupBoxBorrowings.Hide(); // if new member, hide borrowings
            }
            else
            {
                groupBoxBorrowings.Show();
                UpdateBorrowings();
            }
            ShowDialog();
        }


        private void Controls2Fields()
        {
            _properties.Name = textBoxName.Text;
            _properties.Address = textBoxAddress.Text;
            _properties.Contact = textBoxContact.Text;
            _properties.Type = comboBoxType.SelectedIndex;
        }

        private void Fields2Controls()
        {
            textBoxName.Text = _properties.Name;
            textBoxAddress.Text = _properties.Address;
            textBoxContact.Text = _properties.Contact;
            comboBoxType.SelectedIndex = _properties.Type ?? 0;
        }

        public event EventHandler<MemberEventArgs> Submit;

        private void OnSubmit()
        {
            if (Submit != null)
            {
                Submit(this, _properties);
            }
        }

        public event EventHandler<BorrowingEventArgs> ReturnBook;

        private void OnReturnBook()
        {
            if (ReturnBook != null)
            {
                var row = dataGridViewBorrowings.SelectedRows[0];
                int id = Int32.Parse(row.Cells["MBColumnId"].Value.ToString());
                ReturnBook(this, new BorrowingEventArgs(id));
            }
        }

        public event EventHandler Cancel;

        private void OnCancel()
        {
            if (Cancel != null)
            {
                Cancel(this, null);
            }
        }

        public event EventHandler NewBurrow;

        private void OnNewBurrow()
        {
            if (NewBurrow != null)
            {
                NewBurrow(this, null);
            }
        }

        private void MemberEditForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

        private void buttonNewBorrow_Click(object sender, EventArgs e)
        {
            OnNewBurrow();
        }

        private void MemberEditForm_FormClosing_1(object sender, FormClosingEventArgs e)
        {
            SafeClose();
        }

        private void MemberEditForm_Shown(object sender, EventArgs e)
        {
            UpdateBorrowings();
        }

    }
}
